package com.ruoyi.common.utils;


import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;

/**
 * HttpClient工具类
 */
public class HttpClientUtil { 
	
	private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

	public static String sendPost(String urlTail, String json) {
		String content = "";
		CloseableHttpClient httpClient = HttpClients.createDefault();


		HttpPost httpPost = new HttpPost(urlTail);
		// 请求头的设置
		httpPost.setHeader("Accept", "application/json, text/plain, */*");
		httpPost.setHeader("Content-type", "application/json;charset=utf-8");
		// 参数配置 编码格式配置
		StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
		httpPost.setEntity(stringEntity);
		// 执行请求操作，并拿到结果（同步阻塞）
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpPost);
			content = EntityUtils.toString(response.getEntity());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return content;
	}
}
